#!/bin/bash
set -e
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu"
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH

source $SNAP/actions/common/utils.sh

app=$1

if [ "${app}" = "kubelet" ]
then
  # wait for containerd socket
  if grep -e "--address " $SNAP_DATA/args/containerd &> /dev/null
  then
    socket=$(grep -e "--address " $SNAP_DATA/args/containerd | awk '{print $2}')
    # socket_file may be of the form ${SNAP_DATA}/containerd.sock
    # we need to replace any variables
    socket_file_expand=$(eval echo ${socket})
    # wait up until 20 seconds for the docker socket to appear
    n=0
    until [ $n -ge 10 ]
    do
      test -S "${socket_file_expand}" && break
      echo "Waiting for containerd socket ${socket_file_expand} to appear. (attempt $n)"
      n=$[$n+1]
      sleep 2
    done
  fi

  # Handle non-unified cgroups https://github.com/ubuntu/microk8s/issues/519
  if [ -e /proc/$$/cgroup ] &&
     [[ $(awk -F '[:]' '(/cpu/ && !/cpuset/) || /memory/ {print $3}' /proc/$$/cgroup | uniq | wc -l) -eq "2" ]] &&
     ! grep -e "runtime-cgroups" $SNAP_DATA/args/kubelet &> /dev/null &&
     ! grep -e "kubelet-cgroups" $SNAP_DATA/args/kubelet &> /dev/null &&
     [ -e  /sys/fs/cgroup/systemd/system.slice ]
  then
    refresh_opt_in_config "runtime-cgroups" "/systemd/system.slice" kubelet
    refresh_opt_in_config "kubelet-cgroups" "/systemd/system.slice" kubelet
  fi

elif [ "${app}" = "kube-apiserver" ] || [ "${app}" = "kube-proxy" ]
then
  # Check if we advertise an address. If we do we do not need to wait for a default network interface.
  if ! grep -E "(--advertise-address|--bind-address)" $SNAP_DATA/args/kube-apiserver &> /dev/null
  then
    # we keep this command to cleanup the file, as it is no longer needed.
    rm -f ${SNAP_DATA}/external_ip.txt
    # wait up to two minutes for the default network interface to appear.
    n=0
    until [ $n -ge 20 ]
    do
      ip route | grep default &> /dev/null && break
      ip -6 route | grep default &> /dev/null && break
      echo "Waiting for default route to appear. (attempt $n)"
      n=$[$n+1]
      sleep 6
    done

  fi
  if [ -e ${SNAP_DATA}/var/lock/stopped.lock ]
  then
    # Mark the api server as starting. This is needed incase you
    # microk8s.stop and then snap start microk8s
    rm -f ${SNAP_DATA}/var/lock/stopped.lock &> /dev/null
  fi
fi

# This is really the only way I could find to get the args passed in correctly. WTF
declare -a args="($(cat $SNAP_DATA/args/$app))"
if [ -f $SNAP/$app ]
then
  exec "$SNAP/$app" "${args[@]}"
else
  # dockerd is not in $SNAP
  exec "$SNAP/usr/bin/$app" "${args[@]}"
fi
